package com.kyc.kycxkplaygame.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import com.kyc.kycxkplaygame.R;
import com.kyc.kycxkplaygame.dbutils.GetPropLevelUtils;
import com.kyc.kycxkplaygame.util.SharedPreferencesUtils;
import es.dmoral.toasty.Toasty;

public class PauseGameActivity extends Activity {

    private boolean yx_open;
    private boolean yL_open;
    private ImageView pause_yinxiao;
    private ImageView pause_yinliang;
    private ImageView pause_again_btn;
    private String close;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setFinishOnTouchOutside(false);
        setContentView(R.layout.activity_pause_game);
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.alpha = 1f;
        window.setAttributes(layoutParams);



        yx_open = (boolean) SharedPreferencesUtils.getParam(this, "yx_open", true);
        yL_open = (boolean) SharedPreferencesUtils.getParam(this, "yL_open", true);

        pause_yinxiao = findViewById(R.id.pause_yinxiao);
        pause_yinliang = findViewById(R.id.pause_yinliang);
        pause_again_btn = findViewById(R.id.pause_again_btn);

        close = getIntent().getStringExtra("close");
        if(close.equals("close")){
            pause_again_btn.setImageResource(R.drawable.close_now_game_btn);
        }else if(close.equals("pause")) {
            pause_again_btn.setImageResource(R.drawable.pause_again_play_btn);
        }

        if(yx_open) {
            pause_yinxiao.setImageResource(R.drawable.yinxiao_open);
        }else {
            pause_yinxiao.setImageResource(R.drawable.yinxiao_close);
        }

        if(yL_open) {
            pause_yinliang.setImageResource(R.drawable.yinliang_open);
        }else {
            pause_yinliang.setImageResource(R.drawable.yinliang_close);
        }

        findViewById(R.id.pause_activity_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PauseGameActivity.this, GameActivity.class);
                intent.putExtra("pause","");
                setResult(700, intent);
                PauseGameActivity.this.finish();
            }
        });

        pause_again_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(close.equals("close")){
                    Intent intent = new Intent(PauseGameActivity.this, GameActivity.class);
                    intent.putExtra("pause","close");
                    setResult(700,intent);
                    PauseGameActivity.this.finish();
                }else if(close.equals("pause")) {
                    String tili_num = String.valueOf(GetPropLevelUtils.getInstance().getLevel());//(String) SharedPreferencesUtils.getParam(PauseGameActivity.this, "tili_num", "");
                    int xiyou = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("西柚"));//(int) SharedPreferencesUtils.getParam(PauseGameActivity.this, "xiyou", 0);
                    if(Integer.parseInt(tili_num) <= 0) {
                        if(xiyou <= 0) {
                            Toasty.warning(PauseGameActivity.this, "体力不够啦!", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(PauseGameActivity.this, PropsBuyActivity.class);
                            intent.putExtra("daoju", "xiyou");
                            startActivity(intent);
                        }else {
                            Intent intent = new Intent(PauseGameActivity.this, GameActivity.class);
                            intent.putExtra("pause","xiyou");
                            setResult(500, intent);
                            PauseGameActivity.this.finish();
                        }
                    }else if(Integer.parseInt(tili_num) >= 1){
                        Intent intent = new Intent(PauseGameActivity.this, GameActivity.class);
                        intent.putExtra("pause","tili");
                        setResult(500, intent);
                        PauseGameActivity.this.finish();
                    }
                }
            }
        });

        pause_yinxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(yx_open) {
                    pause_yinxiao.setImageResource(R.drawable.yinxiao_close);
                }else {
                    pause_yinxiao.setImageResource(R.drawable.yinxiao_open);
                }
                yx_open = !yx_open;
                SharedPreferencesUtils.setParam(PauseGameActivity.this, "yx_open", yx_open);
            }
        });
        pause_yinliang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(yL_open) {
                    pause_yinliang.setImageResource(R.drawable.yinliang_close);
                }else {
                    pause_yinliang.setImageResource(R.drawable.yinliang_open);
                }
                yL_open = !yL_open;
                SharedPreferencesUtils.setParam(PauseGameActivity.this, "yL_open", yL_open);
            }
        });

        findViewById(R.id.pause_shuoming).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    @Override
    public void onBackPressed() {
        if(close.equals("close")){
            setResult(700,new Intent(PauseGameActivity.this, GameActivity.class));
            PauseGameActivity.this.finish();
        }else if(close.equals("pause")) {
            Intent intent = new Intent(PauseGameActivity.this, GameActivity.class);
            intent.putExtra("pause","");
            setResult(500, intent);
            PauseGameActivity.this.finish();
        }
    }
}
